package com.aisino.api.util;

import com.aisino.api.config.EntityCacheConfig;
import com.aisino.api.enums.EntityHumitureInfoEnum;

/**
 * @description:
 * @author: 陈兵
 * @date: 2025/8/18 18:59
 * @version: 1.0
 */
public class HumitureUtil {

    /**
     * 温度标准值-常温区
     */
    private static final String YZ_WD_BZZ_CW = "YZ_WD_BZZ_CW";
    /**
     * 温度标准值-阴凉区
     */
    private static final String YZ_WD_BZZ_YL = "YZ_WD_BZZ_YL";
    /**
     * 温度标准值-冷藏区
     */
    private static final String YZ_WD_BZZ_LC = "YZ_WD_BZZ_LC";

    /**
     * 湿度标准值-常温区
     */
    private static final String YZ_SD_BZZ_CW = "YZ_SD_BZZ_CW";
    /**
     * 湿度标准值-阴凉区
     */
    private static final String YZ_SD_BZZ_YL = "YZ_SD_BZZ_YL";
    /**
     * 湿度标准值-冷藏区
     */
    private static final String YZ_SD_BZZ_LC = "YZ_SD_BZZ_LC";

    public static Boolean[] checkTemperatureAndHumidityInRange(String sbazqy, Double temperature, Double humidity) {
        // 获取配置的温湿度阈值
        String wdThreshold = null;
        String sdThreshold = null;

        if (EntityHumitureInfoEnum.CW.getValue().equals(sbazqy)) {
            // 常温 温度阈值: 10℃~30℃ 湿度阈值: 35%~75%
            wdThreshold = EntityCacheConfig.YZ_CACHE.get(YZ_WD_BZZ_CW);
            sdThreshold = EntityCacheConfig.YZ_CACHE.get(YZ_SD_BZZ_CW);
        } else if (EntityHumitureInfoEnum.YL.getValue().equals(sbazqy)) {
            // 阴凉 温度阈值: ≤20℃ 湿度阈值: 35%~75%
            wdThreshold = EntityCacheConfig.YZ_CACHE.get(YZ_WD_BZZ_YL);
            sdThreshold = EntityCacheConfig.YZ_CACHE.get(YZ_SD_BZZ_YL);
        } else if (EntityHumitureInfoEnum.LC.getValue().equals(sbazqy)) {
            // 冷藏 温度阈值: 2℃~8℃ 湿度阈值: 35%~75%
            wdThreshold = EntityCacheConfig.YZ_CACHE.get(YZ_WD_BZZ_LC);
            sdThreshold = EntityCacheConfig.YZ_CACHE.get(YZ_SD_BZZ_LC);
        }

        // 解析温度阈值并判断
        boolean isTemperatureInRange = isValueInRange(temperature, wdThreshold, sbazqy, 1);
        // 解析湿度阈值并判断
        boolean isHumidityInRange = isValueInRange(humidity, sdThreshold, sbazqy, 2);

        return new Boolean[] {isTemperatureInRange, isHumidityInRange};
    }

    /**
     * 判断值是否在阈值范围内
     *
     * @param value     待判断的值（温度或湿度）
     * @param threshold 阈值配置字符串
     * @param sbazqy    存储区域类型
     * @return 是否在范围内
     */
    public static boolean isValueInRange(Double value, String threshold, String sbazqy, int type) {
        if (EntityHumitureInfoEnum.LC.getValue().equals(sbazqy) && type == 2) {
            // 冷藏区不检查湿度范围，直接返回true
            return true;
        }

        if (value == null || threshold == null || threshold.isEmpty()) {
            return false;
        }

        try {
            if (EntityHumitureInfoEnum.CW.getValue().equals(sbazqy)) {
                // 常温格式: "10~30" 或 "10~30℃"
                String[] range = threshold.replaceAll("[^0-9.~]", "").split("~");
                if (range.length == 2) {
                    double min = Double.parseDouble(range[0]);
                    double max = Double.parseDouble(range[1]);
                    return value >= min && value <= max;
                }
            } else if (EntityHumitureInfoEnum.YL.getValue().equals(sbazqy)) {
                // 阴凉格式: "≤20" 或 "≤20℃"
                String maxStr = threshold.replaceAll("[^0-9.]", "");
                if (!maxStr.isEmpty()) {
                    double max = Double.parseDouble(maxStr);
                    return value <= max;
                }
            } else if (EntityHumitureInfoEnum.LC.getValue().equals(sbazqy)) {
                // 冷藏格式: "2~8" 或 "2~8℃"
                String[] range = threshold.replaceAll("[^0-9.~]", "").split("~");
                if (range.length == 2) {
                    double min = Double.parseDouble(range[0]);
                    double max = Double.parseDouble(range[1]);
                    return value >= min && value <= max;
                }
            }
        } catch (NumberFormatException e) {
            // 解析数字出错，返回false
            return false;
        }

        return false;
    }
}
